﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Batch_Disc_Backup
{
    /// <summary>
    /// A little confused how the ComboBox methods work, I just created my own ListItem class,
    /// so it could work just like asp.net's ListBox.
    /// </summary>
    public class ListItem
    {
        public String Value { get; set; }
        public String Text { get; set; }

        public ListItem(string text)
        {
            Value = text;
            Text = text;
        }
        public ListItem(string text, string value)
        {
            Value = value;
            Text = text;
        }

        public override string ToString()
        {
            return Text;
        }

        public override bool Equals(object obj)
        {
            return obj is ListItem && ((ListItem)obj).Value == Value;
        }

        public static void SetSelectedValue(ComboBox cbo, string val)
        {
            if (val != null)
            {
                for (int i = 0; i < cbo.Items.Count; i++)
                {
                    if (((ListItem)cbo.Items[i]).Value.Equals(val, StringComparison.OrdinalIgnoreCase))
                    {
                        cbo.SelectedIndex = i;
                        return;
                    }
                }
            }

            cbo.SelectedIndex = -1;
        }

        public static string GetSelectedValue(ComboBox cbo)
        {
            return cbo.SelectedIndex == -1 ? null : ((ListItem)cbo.Items[cbo.SelectedIndex]).Value;
        }
    }
}
